<!--
 * @Date: 2022-01-19 10:33:19
 * @LastEditTime: 2022-11-05 09:41:19
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <script>
    // 给定一个非空整数数组，除了某个元素只出现一次以外，其余每个元素均出现3次，找到那个一次的
    // 输入: [2, 2,2,0,0,0,1]
    // 输出: 1


    // 1.异或
    // 2.hash算法 但是空间是O(n)
    var singleNumber = function (nums) {
      debugger
      let ans = 0;
      for (let i = 0; i < 32; ++i) {
        debugger
        let total = 0;
        for (const num of nums) {
          debugger
          total += ((num >> i) & 1);
        }
        if (total % 3 != 0) {
          debugger
          ans |= (1 << i);
        }
      }
      return ans;
    }
    console.log(singleNumber([2, 2, 2, 0, 0, 0, 1]))
  </script>
</body>

</html>